package codeTop;

public class q124 {
    public int maxPathSum(TreeNode root) {
        dfs(root);
        return max;
    }
    int max = -0x3fffffff;
    int dfs(TreeNode node){
        if (node == null) {
            return 0;
        }
        int left = Math.max(dfs(node.left),0);
        int right = Math.max(dfs(node.right),0);
        int nodeMax = node.val+left+right;
        max = Math.max(nodeMax,max);
        return Math.max(Math.max(left,right)+node.val,0);
    }
}
